﻿function te005(url) {
    $("[data-te001]").live("click", function () {
        var a = $(this);
        var te002 = a.data("te002");
        if (te002 === undefined) {
            $.ajax({
                url: url,
                data: {},
                cache: false,
                success: function (data) {
                    var subNodes = a.siblings("ul");
                    subNodes.hide();
                    subNodes.html(data);
                    subNodes.show("fast");
                    a.data("te002", "te003");
                },
                error: function (jqXHR, textStatus, errorThrow) {
                    alert("Request failed: " + textStatus + ", " + errorThrow);
                }
            });
        } else if (te002 === "te003") {
            a.siblings("ul").hide("fast");
            a.data("te002", "te004");
        } else if (te002 === "te004") {
            a.siblings("ul").show("fast");
            a.data("te002", "te003");
        }
    });
}

function te006(url) {
    $(".Expand_te001").live("click", function () {
        var expandIcon = $(this);
        var node = expandIcon.parent();
        if (node.hasClass("ExpandLeaf_te003")) return;
        if (node.hasClass("ExpandOpen_te004"))
            setOpeningClass(node, "ExpandClosed_te005");
        else if (node.hasClass("ExpandClosed_te005")) {
            if (!node.data("isLoaded_te011"))
                loadNodes(url, node, expandIcon, function () {});
            else
                setOpeningClass(node, "ExpandOpen_te004");
        } else
            throw new Error("invalid opening class");
    });
    $(".Content_te012").live("click", function () {
        selectNode($(this));
    });
}

function selectNode(content) {
    $(".Selected_te016").removeClass("Selected_te016");
    content.addClass("Selected_te016");
    window.location.hash = "#" + content.parent().attr("data-id_te008");
}

function setOpeningClass(node, newClass) {
    $.each(["ExpandOpen_te004", "ExpandClosed_te005", "ExpandLeaf_te003"], function (index, value) {
        return node.removeClass(value);
    });
    node.addClass(newClass);
}

function loadNodes(url, node, expandIcon, afterSuccess) {
    expandIcon.removeClass("Expand_te001");
    expandIcon.addClass("ExpandLoading_te010");
    $.ajax({
        url: url,
        data: {
            id_te009: node.attr("data-id_te008")
        },
        cache: false,
        success: function (data) {
            node.children("ul").remove();
            node.append(data);
            var childrenContainer = node.children("ul");
            if (childrenContainer.children().length === 0) {
                setOpeningClass(node, "ExpandLeaf_te003");
                childrenContainer.remove();
            } else
                setOpeningClass(node, "ExpandOpen_te004");
            node.data("isLoaded_te011", true);
            afterSuccess();
        },
        error: function (jqXHR, textStatus, errorThrow) {
            alert("Request failed: " + textStatus + ", " + errorThrow);
        },
        complete: function () {
            expandIcon.removeClass("ExpandLoading_te010");
            expandIcon.addClass("Expand_te001");
        }
    });
}

function jumpTo_te017(url, nodeIds) {
    loadedTreeWalk(url, nodeIds, 0, null);
}

function loadedTreeWalk(url, nodeIds, index, selectorContext) {
    var node = getNode(nodeIds[index], selectorContext);
    if (index >= nodeIds.length - 1)
        selectNode(node.children(".Content_te012"));
    else if (node.data("isLoaded_te011") !== true) {
        loadNodesRecursively(url, nodeIds, index, node);
    } else {
        if (node.hasClass("ExpandClosed_te005"))
            setOpeningClass(node, "ExpandOpen_te004");
        loadedTreeWalk(url, nodeIds, index + 1, node);
    }
}

function loadNodesRecursively(url, nodeIds, index, node) {
    loadNodes(url, node, node.children(".Expand_te001"), function() {
        var newIndex = index + 1;
        var newNode = getNode(nodeIds[newIndex], node);
        if (newIndex >= nodeIds.length - 1)
            selectNode(newNode.children(".Content_te012"));
        else
            loadNodesRecursively(url, nodeIds, newIndex, newNode);
    });
}

function getNode(id, selectorContext) {
    return $("[data-id_te008='" + id + "']:first", selectorContext);
}